package passengers.demo;

import java.util.ArrayList;

import presence.demo.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import database.demo.DataBaseWrapperPassengers;
import database.demo.PassengersOperations;

/**
 * sending mail to multiple contacts
 */
public class MultipleMailMainActivity extends Activity {
	private PassengersOperations passengerDBoperation;
	ArrayList<String> phoneNo = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multiple_mail);
		// closes keyboard
		this.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		//
		passengerDBoperation = new PassengersOperations(this);
		passengerDBoperation.open();
		//populatePassengerList();
		//addButtonListeners();
	}

	// get all passengers from DB
	private void populatePassengerList() {
		ListView myList1 = (ListView) findViewById(R.id.sms_list1);
		Cursor cursor = passengerDBoperation.getAllPassengers();
/**
		// Allow activity to manage lifetime of the cursor.
		// DEPRECATED! Runs on the UI thread, OK for small/short queries.
		startManagingCursor(cursor);

		// Setup mapping from cursor to view fields:
		String[] fromFieldNames = new String[] {
				DataBaseWrapperPassengers.PASSENGER_LASTNAME,
				DataBaseWrapperPassengers.PASSENGER_FIRSTNAME,
				DataBaseWrapperPassengers.PASSENGER_EMAIL };
		int[] toViewIDs = new int[] { R.id.last_sms, R.id.first_sms,
				R.id.phone_sms };

		// Create adapter to may columns of the DB onto elements in the UI.
		SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, // Context
				R.layout.multi_sms_item_view, // Row layout template
				cursor, // cursor (set of DB records to map)
				fromFieldNames, // DB Column names
				toViewIDs // View IDs to put information in
		);
		// Set the adapter for the list view
		myList1.setAdapter(myCursorAdapter);
**/
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		passengerDBoperation.close();
	}

	public void addButtonListeners() {
		final ListView myList = (ListView) findViewById(R.id.sms_list1);
		Button btnMultiSend = (Button) findViewById(R.id.btn_multi_send);
		Button btnClear = (Button) findViewById(R.id.btn_multi_clear1);
		Button btnSelectAll = (Button) findViewById(R.id.btn_multi_all);

		// Select All
		btnSelectAll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int count = myList.getAdapter().getCount();
				for (int i = 0; i < count; i++) {
					RelativeLayout itemLayout = (RelativeLayout) myList
							.getChildAt(i); // Find by under RelativeLayout
					CheckBox checkbox = (CheckBox) itemLayout
							.findViewById(R.id.check_sms);
					checkbox.setChecked(true);
				}
			}
		});

		// Clear All
		btnClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText smsText = (EditText) findViewById(R.id.sms_text);
				smsText.setText("");
				int count = myList.getAdapter().getCount();
				for (int i = 0; i < count; i++) {
					RelativeLayout itemLayout = (RelativeLayout) myList
							.getChildAt(i); // Find by under RelativeLayout
					CheckBox checkbox = (CheckBox) itemLayout
							.findViewById(R.id.check_sms);
					checkbox.setChecked(false);
				}
			}
		});

		// send multi message
		btnMultiSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				phoneNo.clear(); // empty phone's list
				int count = myList.getAdapter().getCount();
				for (int i = 0; i < count; i++) {
					RelativeLayout itemLayout = (RelativeLayout) myList
							.getChildAt(i); // Find by under LinearLayout
					CheckBox checkbox = (CheckBox) itemLayout
							.findViewById(R.id.check_sms);
					TextView phoneSMS = (TextView) itemLayout
							.findViewById(R.id.phone_sms);
					if (checkbox.isChecked()) {
						if (phoneSMS.getText().length() > 0) {
							phoneNo.add(phoneSMS.getText().toString());
						} else {
							TextView last = (TextView) itemLayout
									.findViewById(R.id.last_sms);
							TextView first = (TextView) itemLayout
									.findViewById(R.id.first_sms);
							AlertDialog alertDialog = new AlertDialog.Builder(
									MultipleMailMainActivity.this).create();
							alertDialog.setTitle("Error");
							alertDialog.setMessage(last.getText() + " "
									+ first.getText()
									+ " please check phone! \nmessage not send");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											// checkbox.requestFocus();
										}
									});
							alertDialog.setIcon(R.drawable.ic_launcher);
							alertDialog.show();
						}
					}
				}
				sendMultiSMS();
			}
		});
	}

	public void sendMultiSMS() {
		SmsManager sms = SmsManager.getDefault();
		EditText messageTxt = (EditText) findViewById(R.id.sms_text);
		if (messageTxt.getText().length() > 0) {
			for (int i = 0; i < phoneNo.size(); i++) {
				sms.sendTextMessage(phoneNo.get(i), null, messageTxt.getText()
						.toString(), null, null);
			}

			Toast.makeText(MultipleMailMainActivity.this,
					"Mail sent successfully", Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(MultipleMailMainActivity.this,
					"Please enter message", Toast.LENGTH_LONG).show();
		}

	}

}
